package com.hjw.weixun.dao.impl;

import java.util.List;

import com.hjw.weixun.Template.JdbcTemplate;
import com.hjw.weixun.dao.WeixunDao;
import com.hjw.weixun.domain.User;
import com.hjw.weixun.domain.Weixun;

public class WeixunDaoJdbcImpl extends JdbcTemplate<Weixun> implements
		WeixunDao {

	public static WeixunDao weixunDao = null;

	// 单例
	public static WeixunDao getInstance() {
		if (weixunDao == null) {
			weixunDao = new WeixunDaoJdbcImpl();
		}
		return weixunDao;
	}

	@Override
	public int addWeixun(Weixun weixun) {
		String name = weixun.getName();
		String introduction = weixun.getIntroduction();
		String path = weixun.getPath();

		String sql = "insert into weixun(name,introduction,path,userid) values (?,?,?,?) ";
		Object[] args = new Object[] { name, introduction ,path,weixun.getUser().getUserid()};
		return this.execute(sql, args);

	}

	@Override
	public Weixun getWeixun(Weixun weixun) {

		String sql = "SELECT * FROM weixun WHERE id = ?";

		Object[] args = new Object[] { weixun.getId() };

		return this.uniqueResult(sql, args);

	}
	
	@Override
	public Weixun getWeixunByPath(Weixun weixun){
		String sql = "SELECT * FROM weixun WHERE path = ?";

		Object[] args = new Object[] { weixun.getPath() };

		return this.uniqueResult(sql, args);
	}

	@Override
	public List<Weixun> getCreatedWeixunList(Integer userId) {

		String sql = "SELECT * FROM weixun WHERE userid = ?";

		Object[] args = new Object[] { userId };

		return this.executeQueryAllToList(sql, args);

	}

	@Override
	public List<Weixun> getFollowingList(Integer userId) {

		String sql = "SELECT * FROM weixun w JOIN follower f ON w.id = f.weixunid  AND f.followerid = ?";
		Object[] args = new Object[] { userId };

		return this.executeQueryAllToList(sql, args);
	}

	@Override
	public List<Weixun> getInterestedList(Integer userId) {
		String sql = "SELECT * FROM weixun WHERE id NOT IN(SELECT weixunid from follower  WHERE followerid = ?) AND userid <> ?";
		Object[] args = new Object[] { userId, userId };

		return this.executeQueryAllToList(sql, args);
	}

	@Override
	public void updateWeixunLogo(Weixun weixun) {
		
		String sql = "update weixun set logo=? where id=? ";
		Object[] args = new Object[] {  weixun.getLogo() ,weixun.getId()};
		this.executeUpdate(sql, args);
		
	}
	
	

	

}
